package com.webex.mirage;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.robochina.airobot.tank.Bot;
import org.robochina.airobot.tank.Bullet;
import org.robochina.airobot.tank.FireAction;
import org.robochina.airobot.tank.HitRobotAction;
import org.robochina.airobot.tank.MessageReceivedAction;
import org.robochina.airobot.tank.RobotDeathAction;
import org.robochina.airobot.tank.RoundBeginAction;
import org.robochina.airobot.tank.SimpleRobot;
import org.robochina.airobot.tank.TickAction;
import org.robochina.math.MathUtils;

import com.webex.util.BaseBullet;
import com.webex.util.PatternMatchFire;
import com.webex.util.TankCommon;

/**
 * @deprecated
 * @author angusl
 *
 */
public class MirageBaseRobot extends SimpleRobot {

	public static final String ROLL_COMMANDER = "Commander";
	public static final String ROLL_WARRIOR = "Warrior";

	protected final double attach_radius = 150.0;
	protected double distance_between_target = 0.0;
	protected double angle_with_target = 0.0;

	protected PatternMatchFire pmf = new PatternMatchFire();
	protected double power = MAX_POWER;
	// protected double min_radius = 50.0;

	// 目标坦克
	protected Bot target = null;

	// 子弹列表
	protected List bullets = new ArrayList();

	// 当前的角色: Commander or Warrior
	protected String roll = ROLL_WARRIOR;

	public Bot getTarget() {
		return this.target;
	}

	/**
	 * 选择进攻目标
	 */
	protected void selectTarget() {
		println("SELECT TARGET");
		List friends = getTeammates();
		List enemys = getOpponents();
		double[] distances = new double[enemys.size()];
		double min_distance = Double.MAX_VALUE;

		for (int iE = 0; iE < enemys.size(); iE++) {
			double totalDistance = 0.0;
			Bot enemy = (Bot) enemys.get(iE);
			totalDistance += TankCommon.distance(getSelfBot(), enemy);
			for (int iF = 0; iF < friends.size(); iF++) {
				Bot friend = (Bot) friends.get(iF);
				totalDistance += TankCommon.distance(enemy, friend);
			}

			if (totalDistance < min_distance) {
				min_distance = totalDistance;
				target = enemy;
			}
		}
		println(target);
	}

	/**
	 * 每时间片触发的事件
	 */
	public void onTick(TickAction action) {
		move(10);println("Tick");
//		if ((target == null || !isAlive(target)) && isCommander()) {
//			selectTarget();
//			broadcastMessage("Target:" + target.getId());
//		} 
//		
//		
//
//			// 记录目标坦克坦克的信息
//			pmf.record(target);
//
//			// 计算和目标之间的距离，及角度
//			distance_between_target = MathUtils.distance(getLocation(), target
//					.getLocation());
//			angle_with_target = MathUtils.heading(getLocation(), target
//					.getLocation());
//			if (distance_between_target > attach_radius) {
//				// 向目标移动
//				
//				moveTo(target.getLocation());
//				
//			} else {
//				// 躲避子弹选择开火
//				patternfire();
//				moveTo(target.getLocation());
//			}
//			
//			//avoidBullet();
			
		
		super.onTick(action);
	}

	private void avoidBullet() {
		List bulletSet = getBulletSet();
		List avoidedSet = filterBullets(bulletSet);
	}

	private List filterBullets(List bulletSet) {
		List avoidedSet = new ArrayList(10);
		for(Iterator iter = bulletSet.iterator(); iter.hasNext();) {
			Bullet bullet = (Bullet)iter.next();
			
		}
		return null;
	}

	
	private List getBulletSet() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 
	 * @author William
	 */
	private void patternfire() {
		fire(pmf.getFirePoint(this, power), power);
	}

	/**
	 * 接收到队友消息时触发
	 */
	public void onMessageReceived(MessageReceivedAction action) {
		String msg = action.getMessage();

		String command = msg.substring(0, msg.indexOf(":"));
		String arg = msg.substring(msg.indexOf(":") + 1);
		Bot target = getBot(Integer.parseInt(arg));
		if (target != null) {
			this.target = target;
		}

		super.onMessageReceived(action);
	}

	/**
	 * 
	 */
	protected void updateBullets() {
		for (int i = bullets.size() - 1; i >= 0; i--) {
			BaseBullet bullet = (BaseBullet) bullets.get(i);
			if (!bullet.isValid(getTime(), getLocation()))
				bullets.remove(i);
		}
	}

	/**
	 * 任何一辆坦克开火的时候
	 */
	public void onFire(FireAction action) {

		BaseBullet bullet = new BaseBullet(action.getBullet(), getSelfBot());
		bullets.add(bullet);

		super.onFire(action);
	}

	/**
	 * 
	 */
	public void onRoundBegin(RoundBeginAction action) {
		bullets.clear();
		target = null;
		//roll = ROLL_WARRIOR;
	}

	public void onRobotDeath(RobotDeathAction action) {
		int deadID = action.getDied();
		Bot deadBot = getBot(deadID);
		if (isTeammate(deadBot)) {
			MirageBaseRobot dBot = (MirageBaseRobot) deadBot;
			if (dBot.isCommander()) {
				boolean nextCommander = amINextCommander();
			}
		} else {

			if (deadBot.getId() == target.getId() && isCommander()) {
				selectTarget();
				broadcastMessage("Target:" + target.getId());
			}
		}
		super.onRobotDeath(action);
	}

	public void onHitRobot(HitRobotAction action) {
		super.onHitRobot(action);
	}

	/**
	 * 是否为Commander
	 * 
	 * @author angusl
	 * @return
	 */
	public boolean isCommander() {
		return ROLL_COMMANDER.equals(this.roll);
	}

	/**
	 * @author calviny
	 */
	public void getTargetScope(BaseBullet bullet) {
		double enemyHeading = MathUtils.heading(getLocation(), target
				.getLocation());

		double distance = MathUtils.distance(getLocation(), target
				.getLocation());

		double angle = Math.asin(attach_radius / distance);

		double minHeading = enemyHeading - angle;

		double maxHeading = enemyHeading + angle;

		double farestDist = attach_radius + distance;
		
		Point2D point = bullet.getLocation(getTime());
		
		
	}

	/**
	 * 判断自己是否是下一个Commander
	 * 
	 * @return
	 * @author angusl
	 */
	public boolean amINextCommander() {
		int minId = getSelf();
		List teammaters = getTeammates();
		for (int i = 0; i < teammaters.size(); i++) {
			Bot bot = (Bot) teammaters.get(i);
			if (isAlive(bot) && bot.getId() < minId) {
				// 如果有一个活着的坦克的id比自己小。退出函数
				return false;
			}
		}
		// 否则自己就是下一个Commander
		this.roll = ROLL_COMMANDER;
		return true;
	}
}
